package com.pms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pms.dto.RepairPageQueryDTO;
import com.pms.dto.RepairSaveDTO;
import com.pms.dto.RepairUpdateDTO;
import com.pms.entity.Repair;
import com.pms.mapper.RepairMapper;
import com.pms.result.PageResult;
import com.pms.service.RepairService;
import com.pms.vo.RepairPageQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.Phaser;

@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairMapper repairMapper;

    /**
     * 新增报修
     * @param repairSaveDTO
     */
    public void save(RepairSaveDTO repairSaveDTO) {
        Repair repair=new Repair();
        BeanUtils.copyProperties(repairSaveDTO,repair);
        repair.setStatusId(8); // 默认未处理
        repair.setCreateDate(LocalDate.now());
        repairMapper.save(repair);
    }

    /**
     * 报修分页查询
     * @param repairPageQueryDTO
     * @return
     */
    public PageResult page(RepairPageQueryDTO repairPageQueryDTO) {
        PageHelper.startPage(repairPageQueryDTO.getPage(), repairPageQueryDTO.getPageSize());
        Page<RepairPageQueryVO> page=repairMapper.page(repairPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            repairMapper.delete(id);
        }
    }

    /**
     * 修改报修状态
     * @param repairUpdateDTO
     */
    public void update(RepairUpdateDTO repairUpdateDTO) {
        Repair repair=repairMapper.getById(repairUpdateDTO.getId());
        repair.setStatusId(repairUpdateDTO.getStatusId());
        repairMapper.update(repairUpdateDTO);
    }
}
